<?php
namespace Factory\Controller;
use Mcit\Page;
use Mcit\Rbac;

class AuthController extends BaseController {

	public function node(){
		if (IS_POST) {
			$sqlData = array();
			$sqlData ['module'] = 'Admin';
			$sqlData ['title']  = I('title');
			switch( I('level') ){
				case 'Action':
					$sqlData ['name']   = $sqlData ['module'].'-'.I('Controller').'-'.I('Action');
					$sqlData ['controller'] = I('Controller');
					$sqlData ['action'] = I('Action');
					$sqlData ['level']  = 3;
					$sqlData ['code'] = md5( strtoupper($sqlData ['module'].$sqlData ['controller'].$sqlData ['action']) );
					break;
				case 'Controller':
					$sqlData ['name']   = $sqlData ['module'].'-'.I('Controller');
					$sqlData ['controller'] = I('Controller');
					$sqlData ['level']  = 2;
					$sqlData ['code'] = md5( strtoupper($sqlData ['module'].$sqlData ['controller']) );
					break; 
			}

			$result = M('node')->add($sqlData);
			if($result){
				$return['state'] = 100;
	            $return['msg']   = "添加成功";
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "添加失败";
	        }
	        echo json_encode($return);
	        exit;
		}
		$this->display();
	}

	public function role(){
		$this->newid = $_GET['newid'];
		$roleData = M('role')->order('date desc')->select();
		$this->roleData = $roleData;
		$this->display();
	}

	public function addRole(){
		$name = I('name');
		$map['name'] = $name;
		if(M('role')->where($map)->find()){
			$return['state'] = 200;            
            $return['msg']   = "角色名称已经使用";
	        echo json_encode($return);
	        exit;
		}
		$sqlData['name']     = $name;
		$sqlData['date']     = date('Y-m-d H:i:s');
		$result = M('role')->where($map)->add($sqlData);
		if($result){
			$return['state'] = 100;
            $return['msg']   = "添加成功,请设置该角色权限";
            $return['url']   = U("auth/role",array('newid'=>$result));
        }else{
            $return['state'] = 200;            
            $return['msg']   = "设置失败";
        }
        echo json_encode($return);
        exit;
	}

	public function delRole(){
		$map['id'] = I('id');
		$result = M('role')->where($map)->delete();
		if($result){
			$return['state'] = 100;
            $return['msg']   = "删除角色成功";
        }else{
            $return['state'] = 200;            
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
	}

	public function access(){
		$role = url_base64_decode ( I('role') );
		$map['id'] = $role;
		$roleInfo  = M('role')->where($map)->find();
		$authInfo  = explode(",", $roleInfo['authcode']); 
		$whereMap['level']  = array('neq',1);
		$result = M('node')->where($whereMap)->order('level')->select();
		$nodeTree = array();
		foreach($result as $key=>$value){
			if($value['level']==2){
				$nodeTree[$value['controller']]['title']=$value['title'];
				$nodeTree[$value['controller']]['name'] =$value['name'];
			}else{
				if( in_array($value['code'],$authInfo) ){
	                $value['checked'] = 1;
	            }else{
	            	$value['checked'] = 0;
	            }
				$nodeTree[$value['controller']]['detail'][]=$value;
			}
		}
		$this->roleInfo = $roleInfo;
		$this->nodeTree = $nodeTree;
		if(session('administrator')){
			$this->administrator = 1;
		}else{
			$this->administrator = 0;
		}
		$this->display();

	}

	public function setAccess(){
		$role = I('role');
		$map['id'] = $role;
		$sqlData['authstr']  = I('authstr');
		$sqlData['authcode'] = I('authcode');
		$sqlData['date']     = date('Y-m-d H:i:s');
		$result = M('role')->where($map)->save($sqlData);
		if($result){
			$return['state'] = 100;
            $return['msg']   = "设置成功";
            $return['url']   = U("auth/role");
        }else{
            $return['state'] = 200;            
            $return['msg']   = "设置失败";
        }
        echo json_encode($return);
        exit;
	}

	public function user(){
		$map['account'] = array('neq','root');
		if(!empty($_GET['name'])){
			$map['name'] = array('like','%'.I('name').'%');
		}

		if(!empty($_GET['account'])){
			$map['account'] = array('like','%'.I('account').'%');
		}

		$count = D('Admin/User')->where($map)->count();
		$Page  = new Page($count);
		// 重搜一律回第一页
		if( $_GET['fsign'] == "abnormal" ){
		 	$_GET['fsign'] = "normal";
		 	$_GET['page']  = 1;
		}
		$nowPage = isset($_GET['page']) ? $_GET['page'] : 1 ;
		$user = D('Admin/User')->relation(true)->where($map)->page($nowPage.','.$Page->listRows)->select();
        foreach($user as $k=>$v){
            $company = M("company")->where("id='".$v['company']."'")->find();
            $user[$k]['company'] = $company['name'];
        }
		$this->pageView = $Page->show();
		$this->user  = $user;
		$this->display();

	}
    //添加账号
	public function addUser(){
		if(IS_POST){
			$account = I('account');
			if(is_numeric($account)){
				$return['state'] = 200;            
	            $return['msg']   = "账号不能全是数字";
		        echo json_encode($return);
		        exit;
			}

			$map['account'] = $account;
			
			if(M('user')->where($map)->find()){
				$return['state'] = 200;            
	            $return['msg']   = "该账号已经使用";
		        echo json_encode($return);
		        exit;
			}

            $tmp = explode("#", I('department'));
            $sqlData['company']  = $tmp[1];
            $sqlData['major_str']   = $tmp[0];
            $tmps = explode("#", I('role'));
			$sqlData['department']  = $tmps[1];
            $sqlData['role']  = $tmps[0];
			$sqlData['password']    = sha1('dz123456');
			$sqlData['account']     = I('account');
			$sqlData['name']        = I('name');
			$sqlData['create_time'] = date('Y-m-d H:i:s');
			$result = M('user')->where($map)->add($sqlData);
			if($result){
				$return['state'] = 100;
	            $return['msg']   = "添加成功";
	            $return['url']   = U("auth/user",array('newid'=>$result));
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "添加失败";
	        }
	        echo json_encode($return);
	        exit;
		}else{
            $map['state'] = 1;
            $school = D('Admin/Company')->where($map)->order('id desc')->relation(true)->select();
            $department = array();
            foreach ($school as $key => $value) {
                $department[$key]['id'] = $value['id'];
                $department[$key]['name'] = $value['name'];
                foreach($value['major'] as $k=>$v){
                    $department[$key]['major_str'] .= empty($department[$key]['major_str']) ? $v['id'] : ','.$v['id'];
                }
            }
            $this->department = $department;
			$roleInfo = M('role')->where("1")->select();
			$this->roleInfo = $roleInfo;
			$this->display();
		}
	}
	public function editUser(){
		if(IS_POST){
			$map['id']              = I('uid');
            $sqlData['department']  = I('department');
			$sqlData['role']        = I('role');
			$sqlData['account']     = I('account');
			$sqlData['name']        = I('name');
			$sqlData['create_time'] = date('Y-m-d H:i:s');
			$result = M('user')->where($map)->save($sqlData);
			if($result){
				$return['state'] = 100;
	            $return['msg']   = "修改成功";
	            $return['url']   = U("auth/user",array('newid'=>$result));
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "修改失败";
	        }
	        echo json_encode($return);
	        exit;
		}else{
			$id = I('id');
			$mapx['id'] = $id;
			$user = M('user')->where($mapx)->find();
			$this->user = $user;
			//$maprole['id'] = array('neq',777);
			$roleInfo = M('role')->where(1)->select();
			$this->roleInfo = $roleInfo;
			$this->display();
		}
	}

	public function delUser(){
		$map['id'] = I('id');
		$result = M('user')->where($map)->delete();
		if($result){
			$return['state'] = 100;
            $return['msg']   = "删除账号成功";
        }else{
            $return['state'] = 200;            
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
	}
}